﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agile.Engine.View;
using System.Collections;
using Agile.Engine;
using Agile.Engine.Data;
using System.Data;

namespace Fis.Win.Rules
{
    public class AccountEditViewProvider : ClientViewProvider
    {
        public override Hashtable InitializeViewModel(Hashtable args)
        {
            if (args != null && ViewState == Agile.Engine.View.ViewState.AddNew)
            {
                return args;
            }

            return base.InitializeViewModel(args);
        }

        public override bool ExecuteValidate(Hashtable viewModel)
        {

            if (View.ViewState == Agile.Engine.View.ViewState.AddNew)
            {
                DataAccess a = new DataAccess() { Context = DataContext, TableName = DataTableName };
                var d = viewModel["viewData"] as DataTable;


                if (0 < DataContext.DefaultModel.Count(DataTableName, "FCODE", string.Format("FPARENT = '{0}' AND FCODE='{1}'", d.Rows[0]["FPARENT"], d.Rows[0]["FCODE"])))
                {
                    View.Message = "编码已存在。";
                    return false;
                }

                if (0 < DataContext.DefaultModel.Count(DataTableName, "FNAME", string.Format("FPARENT = '{0}' AND FNAME='{0}'", d.Rows[0]["FPARENT"], d.Rows[0]["FNAME"])))
                {
                    View.Message = "名称已存在。";
                    return false;
                }
            }

            return true;
        }

        public override bool ExecuteUpdate(Hashtable viewModel)
        {
            ///验证失败
            if (!ExecuteValidate(viewModel))
            {
                return false;
            }

            var param = Fis.Win.Utils.AppSession.CreateServiceParamter(viewModel);
            param["viewData"] = Agile.Engine.Utils.ZipUtils.ZipDataTable(viewModel["viewData"] as DataTable);

            var exec = InvokeService(param);
            param["viewData"] = Agile.Engine.Utils.ZipUtils.UnZipDataTable(viewModel["viewData"].ToString());
            if (exec)
            {
                var data = Agile.Engine.Utils.ZipUtils.UnZipDataTable(ServiceProcess.Result["viewData"].ToString());
                DataAccess a = new DataAccess() { Context = DataContext, TableName = DataTableName };

                if (ViewState == Agile.Engine.View.ViewState.AddNew)
                {
                    DataContext.DefaultModel.ExecuteTransaction(
                        () =>
                        {
                            if (ServiceProcess.Result.ContainsKey("ISFIRSTCHILD"))
                            {
                                var xml = Agile.Engine.ObjectContext.GetResource("SqlResource");
                                var bill = xml.Format("newAccountRefreashBill", data.Rows[0]["SID"], data.Rows[0]["FPARENT"]);
                                //var rest = xml.Format("newAccountRefreashBill", data.Rows[0]["SID"], data.Rows[0]["FPARENT"]);
                                DataContext.DefaultModel.ExecuteNonQuery(bill);
                                //DataContext.DefaultModel.ExecuteNonQuery(rest);

                                ///包含余额表数据
                                if (ServiceProcess.Result.ContainsKey("TREST"))
                                {
                                    var rest = Agile.Engine.Utils.ZipUtils.UnZipDataTable(ServiceProcess.Result["TREST"].ToString());
                                    var restda = new DataAccess() { Context = DataContext, TableName = "TREST" };
                                    restda.AddNew(rest.Select());
                                }

                            }
                            a.AddNew(data.Rows[0]);
                        });

                }
                else
                {
                    a.Update(data.Rows[0]);
                }

                viewModel["viewData"] = data;
            }

            View.Message = ServiceProcess.Message;
            return exec;
        }

        public override bool UpdateLocal(Hashtable args)
        {
            var d = args["viewData"] as DataTable;

            DataAccess a = new DataAccess() { Context = DataContext, TableName = DataTableName };

            var r = d.Rows[0];
            ///没有启用服务，需要生成ID
            if (IsEnableService)
            {
            }

            if (View.ViewState == Agile.Engine.View.ViewState.AddNew)
            {
                r["SID"] = Guid.NewGuid().ToString();
                a.AddNew(r);
            }
            else
            {
                a.Update(r);
            }
            return true;
        }

    }
}
